import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HBase_Facility {
    static Configuration config = HBaseConfiguration.create();
    static String zkAddress = "hb-proxy-pub-bp15y52r36ve20x4n-001.hbase.rds.aliyuncs.com:2181";
    static Connection connection = null;
    static{
        config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
        try {
            connection = ConnectionFactory.createConnection(config);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //创建表传入表名与列簇
    public static void Create_table(String table,String[] cf_default) throws IOException {
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(table));
        for(int i = 0;i < cf_default.length;i++){
            tableDescriptor.addFamily(new HColumnDescriptor(cf_default[i]));   //创建列簇
        }
        System.out.print("Creating table. ");
        Admin admin = connection.getAdmin();
        admin.createTable(tableDescriptor);   //创建表
        System.out.println(" Done.");
    }

    //插入数据传入表名，行键，内容，列簇和列名
    public static void insert_value(String Table,String rowkey,String value,String cf,String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(Table));
        try {
            Put put = new Put(rowkey.getBytes());
            put.addColumn(cf.getBytes(), col.getBytes(), value.getBytes());    //添加数据
            table.put(put);
            Get get = new Get(rowkey.getBytes());
            Result r = table.get(get);
            byte[] b = r.getValue(cf.getBytes(), col.getBytes());  // returns current version of value
            System.out.println(new String(b));
        } finally {
            if (table != null) table.close();
        }
    }

    //扫描全表传入表名
    public static String[] get_table(String tableName) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));
        String[] ans = null;
        try {
            //Scan所有数据
            Scan scan = new Scan();
            ResultScanner rss = table.getScanner(scan);
            for(Result r:rss){
                System.out.println("\n row: "+new String(r.getRow()));
                String tmp = new String(r.getRow(),"utf-8");
                ans = insert(ans,tmp);
            }
            rss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ans;
    }

    //获取数据传入表名和行键
    public static String get_rowvalue(String tableName,String rowkey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        String ans = null;
        try {
            Get get = new Get(rowkey.getBytes()); //根据主键查询
            Result r = table.get(get);
            System.out.println("row: "+new String(r.getRow(),"utf-8"));
            ans = new String(r.getRow(),"utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ans;
    }

    //获取数据表名，行键与列簇
    public static String get_rowcolvalue(String tableName,String rowkey,String column) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        String ans = null;
        try {
            Get get = new Get(rowkey.getBytes());
            get.addFamily(column.getBytes()); //根据主键查询某列簇
            Result r = table.get(get);
            byte[] b = r.value();  //获取数据
            System.out.println(new String(b));
            ans = new String(b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ans;
    }

    //获取数据传入表名，行键，列簇和列名
    public static String get_value(String Table,String rowkey,String cf,String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(Table));
        String ans;
        try {
            Get get = new Get(rowkey.getBytes());
            Result r = table.get(get);
            byte[] b = r.getValue(cf.getBytes(), col.getBytes());  //获取数据
            System.out.println(new String(b));
            ans = new String(b);
        } finally {
            if (table != null) table.close();
        }
        return ans;
    }

    //读取文件批量插入数据
    public static void insertvalue(String filepath){
        String txt = RWCSV.read_csv(filepath);
        String table = "";
        String rowkey = "";
        String value = "";
        String cf = "";
        String col = "";
    }

    //给数组添加数据
    private static String[] insert(String[] arr, String str) {
        int size = arr.length;  //获取数组长度
        String[] tmp = new String[size + 1];  //新建临时字符串数组，在原来基础上长度加一
        for (int i = 0; i < size; i++){  //先遍历将原来的字符串数组数据添加到临时字符串数组
            tmp[i] = arr[i];
        }
        tmp[size] = str;  //在最后添加上需要追加的数据
        return tmp;  //返回拼接完成的字符串数组
    }

    //搭建索引
    private static void set_
}
